//
//  TodayViewController.m
//  TodayWidget
//
//  Created by yao zhaofeng on 2018/4/16.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import "DataUtil.h"

@interface TodayViewController () <NCWidgetProviding>

@end

@implementation TodayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"");
    // Do any additional setup after loading the view from its nib.
    
    NSError *error = nil;
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.xy.3DTouch"];
    containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/array"];
    NSArray *arr = [NSArray arrayWithContentsOfURL:containerURL];
    NSLog(@"arr is %@", arr);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"");
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"");
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"");
}

- (void)loadView {
    [super loadView];
    NSLog(@"");
    //配置Today Extension展示视图的大小
    self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 100);
    NSInteger width = 52.0f;
    int kBtnTitleBottomInset = -90.0f;
    
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.xy.3DTouch"];
    containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/iconArr"];
    NSArray *arr = [NSArray arrayWithContentsOfURL:containerURL];
    UIImage *image1 = [UIImage imageWithData:arr[0][@"imgData"]];
    UIImage *image2 = [UIImage imageWithData:arr[1][@"imgData"]];
    
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, width, width)];
//    NSString *title = [DataUtil readDataFromNSUserDefaultsForKey:@"Widget"];
    NSString *title = arr[0][@"title"];
    title = title && title.length > 0 ? title : @"打开应用";
    [btn1 setTitle:title forState:UIControlStateNormal];
//    btn1.backgroundColor = [UIColor redColor];
//    [btn1 setImage:image1 forState:UIControlStateNormal];
    [btn1 setBackgroundImage:image1 forState:UIControlStateNormal];
    btn1.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, kBtnTitleBottomInset, 0)];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:12];
    btn1.tag = 200;
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];

    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(120, 10, width, width)];
//    title = [DataUtil readDataByNSFileManagerForPath:@"Library/Caches/test"];
    title = arr[1][@"title"];
     title = title && title.length > 0 ? title : @"应用内弹窗";
    [btn2 setTitle:title forState:UIControlStateNormal];
//    btn2.backgroundColor = [UIColor redColor];
     [btn2 setBackgroundImage:image2 forState:UIControlStateNormal];
    btn2.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, kBtnTitleBottomInset, 0)];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:12];
    btn2.tag = 201;
    [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
//    UILabel *lbl = [[UILabel alloc] init];
//    lbl.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100);
//    lbl.text = @"你好~";
//    [self.view addSubview:lbl];

}

- (void)btnClick:(UIButton *)sender {
    NSInteger tag = sender.tag;
    NSString *title = [sender currentTitle];
    title = title ? title : @"默认值";
    
    if (tag == 200) {
        //打开应用
        //scheme为app的scheme
        [DataUtil saveDataByNSUserDefaults:title forKey:@"Widget"];
        [self.extensionContext openURL:[NSURL URLWithString:@"3DTouch://hello?gameid=1001&productid=3000"]
                     completionHandler:^(BOOL success) {
                         NSLog(@"open url result:%d",success);
                     }];
    } else if (tag == 201) {
        [DataUtil saveDataByNSFileManager:title ForPath:@"Library/Caches/test"];
        [self.extensionContext openURL:[NSURL URLWithString:@"3DTouch://alert"]
                     completionHandler:^(BOOL success) {
                         NSLog(@"open url result:%d",success);
                         }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
    // Perform any setup necessary in order to update the view.
    
    // If an error is encountered, use NCUpdateResultFailed
    // If there's no update required, use NCUpdateResultNoData
    // If there's an update, use NCUpdateResultNewData

    completionHandler(NCUpdateResultNewData);
}

//iOS8.0~10.0有效
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets {
    return UIEdgeInsetsZero;
}
@end
